﻿using System.Data;
using System.Text;
using DevelopTool.Dal;
using DevelopTool.Model.DbModels;

namespace DevelopTool.Bll.DbBlls
{
    /// <summary>
    /// Oracle数据库操作类
    /// </summary>
    public class OracleDbBll
    {
        /// <summary>
        /// 数据库操作类
        /// </summary>
        private OracleDbDal oracleDbDal;

        /// <summary>
        /// 连接信息
        /// </summary>
        private DbInfo dbInfo;


        public OracleDbBll(DbInfo dbInfo)
        {
            this.dbInfo = dbInfo;
            oracleDbDal = new OracleDbDal(GetConnectionString());
        }


        /// <summary>
        /// 获取Oracle数据库连接串
        /// </summary>
        /// <param name="dbInfo"></param>
        /// <returns></returns>
        public string GetConnectionString()
        {
            string connectionString;
            if(!string.IsNullOrEmpty(this.dbInfo.ConnectionString))//优先尝试连接串方式
            {
                //连接串方式
                connectionString = this.dbInfo.ConnectionString;
            }
            else
            {
                //自定义方式
                connectionString = $"Data Source={this.dbInfo.Ip};User ID={this.dbInfo.UserName};Password={this.dbInfo.Password};";
            }
            return connectionString;
        }


        /// <summary>
        /// 测试Oracle数据库连接
        /// </summary>
        /// <param name="connectionConfig"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public bool TestConnect()
        {
            return oracleDbDal.TestConnect();
        }


        /// <summary>
        /// 获取表集合
        /// </summary>
        /// <param name="dataBase">实例名</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public DataTable GetTables(string dataBase = null,string tableName = null)
        {
            var list = oracleDbDal.GetTables(dataBase,tableName);
            return list;
        }

        /// <summary>
        /// 获取表字段集合
        /// </summary>
        /// <param name="dataBase"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public DataTable GetTableColumns(string dataBase = null,string tableName = null,string columnName = null)
        {
            var dataTable = oracleDbDal.GetTableColumns(dataBase,tableName,columnName);
            return dataTable;
        }

        /// <summary>
        /// 获取视图字段集合
        /// </summary>
        /// <param name="dataBase"></param>
        /// <param name="viewName"></param>
        /// <returns></returns>
        public DataTable GetViewColumns(string dataBase = null,string viewName = null,string columnName = null)
        {
            var dataTable = oracleDbDal.GetViewColumns(dataBase,viewName,columnName);
            return dataTable;
        }

        /// <summary>
        /// 查询字段有效的数据条数
        /// </summary>
        /// <param name="dataBase"></param>
        /// <param name="tableName"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public int GetColumnDateRowNum(string dataBase,string tableName,string columnName)
        {
            return oracleDbDal.GetColumnDateRowNum(dataBase,tableName,columnName);
        }


        /// <summary>
        /// 获取视图集合
        /// </summary>
        /// <param name="dataBase"></param>
        /// <returns></returns>
        public DataTable GetViews(string dataBase = null,string viewName = null)
        {
            var list = oracleDbDal.GetViews(dataBase,viewName);
            return list;
        }


        /// <summary>
        /// 获取对象依赖关系数据
        /// </summary>
        /// <param name="dataBase"></param>
        /// <param name="name"></param>
        /// <param name="objType"></param>
        /// <param name="dependenciesType"></param>
        /// <returns></returns>
        public DataTable GetReferences(string dataBase,string name,string objType,int dependenciesType)
        {
            var list = oracleDbDal.GetReferences(dataBase,name,objType,dependenciesType);
            return list;
        }


        /// <summary>
        /// 获取依赖关系数量
        /// </summary>
        /// <param name="objectName"></param>
        /// <param name="owner"></param>
        /// <param name="objectType"></param>
        /// <returns></returns>
        public string GetObjectRefCount(string objectType,string objectName,string owner)
        {
            return oracleDbDal.GetObjectRefCount(objectType,objectName,owner);
        }

        /// <summary>
        /// 获取函数集合
        /// </summary>
        /// <param name="dataBase"></param>
        /// <returns></returns>
        public DataTable GetFuncs(string dataBase = null,string funcName = null)
        {
            var list = oracleDbDal.GetFuncs(dataBase,funcName);
            return list;
        }


        /// <summary>
        /// 获取存储过程集合
        /// </summary>
        /// <param name="dataBase"></param>
        /// <returns></returns>
        public DataTable GetProcedures(string dataBase = null,string proName = null)
        {
            var list = oracleDbDal.GetProcedures(dataBase,proName);
            return list;
        }

        /// <summary>
        /// 获取触发器集合
        /// </summary>
        /// <param name="dataBase"></param>
        /// <returns></returns>
        public DataTable GetTriggers(string dataBase = null,string triggerName = null)
        {
            var list = oracleDbDal.GetTriggers(dataBase,triggerName);
            return list;
        }


        /// <summary>
        /// 获取所有jobs集合
        /// </summary>
        /// <returns></returns>
        public DataTable GetJobs(string dataBase = null)
        {
            var list = oracleDbDal.GetJobs(dataBase);
            return list;
        }

        /// <summary>
        /// 获取同义词集合
        /// </summary>
        /// <param name="dataBase"></param>
        /// <returns></returns>
        public DataTable GetSynonyms(string dataBase = null,string synonymName = null)
        {
            var list = oracleDbDal.GetSynonyms(dataBase,synonymName);
            return list;
        }

        /// <summary>
        /// 获取所有用户集合
        /// </summary>
        /// <returns></returns>
        public DataTable GetUsers()
        {
            var list = oracleDbDal.GetUsers();
            return list;
        }

        /// <summary>
        /// 查询所有资源
        /// </summary>
        /// <returns></returns>
        public DataTable GetSources()
        {
            DataTable dataTable = oracleDbDal.GetSources();

            dataTable.Columns["COMMENTS"].ReadOnly = false;
            dataTable.Columns["COMMENTS"].MaxLength = int.MaxValue;
            for(int i = 0 ; i < dataTable.Rows.Count ; i++)
            {
                if(OracleSourceComment.commentsDic.TryGetValue(dataTable.Rows[i]["SOURCE_NAME"].ToString(),out string comments))
                {
                    dataTable.Rows[i]["COMMENTS"] = comments;//设置注释

                }
            }
            return dataTable;
        }





        /// <summary>
        /// 查询包集合
        /// </summary>
        /// <param name="dataBase"></param>
        /// <returns></returns>
        public DataTable GetPackages(string dataBase = null,string packageName = null)
        {
            var list = oracleDbDal.GetPackages(dataBase,packageName);
            return list;
        }

        /// <summary>
        /// 查询序列
        /// </summary>
        /// <param name="dataBase"></param>
        /// <returns></returns>
        public DataTable GetSequences(string dataBase = null,string sequenceName = null)
        {
            var list = oracleDbDal.GetSequences(dataBase,sequenceName);
            return list;
        }




        /// <summary>
        /// 根据表名获取建表sql（DDL）
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public string GetTypeDDLText(string type,string name,string owner)
        {
            return oracleDbDal.GetTypeDDLText(type,name,owner);
        }



        public DataTable ExecuteSqlSimple(string sql)
        {
            return oracleDbDal.GetDataTable(sql);
        }



        /// <summary>
        /// 获取指定表/视图数据行数
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public int GetRowCount(string tableName)
        {
            return oracleDbDal.GetRowCount(tableName);
        }

        /// <summary>
        /// 获取数据库对象的脚本信息
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public SourceText GetSourceText(string owner,string name,string type)
        {
            DataTable texts = oracleDbDal.GetSourceText(owner,name,type);
            StringBuilder sb = new StringBuilder();
            foreach(DataRow dataRow in texts.Rows)
            {
                sb.Append(dataRow["TEXT"].ToString());
            }
            return new SourceText() { Owner = owner,Object_Name = name,LineNum = texts.Rows.Count,Text = sb.ToString(),SourceType = type };
        }

    }
}
